/*
 * File: vfsdriver.c
 *
 * Description: This is a driver program for testing your VFS system using an interaction script as input
 * You need to make additional calls to your respective functions as noted in the comments below
 * Make sure the output you display is exactly as per the given specifications for you. Do NOT print
 * any extra output (like debug messages) in the final version of your driver program. You can use this driver program
 * in a in incremental manner as you keep implementing one operator after another. For operators not yet implemented,
 * you can leave the output as given ("TO_BE_DONE"). So you can use this program as your "main" program for testing purposes.
 *
 * DO NOT write the full code for operators in the driver program! You must only CALL your functions from here.
 *
 * Usage: vfsdriver <scriptfilename>
 */

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

#include "..//include//createvfs.h"
#include "..//include//n_ary.h"
#include "..//include//vfs_errorcodes.h"
#include "..//include//folderOperations.h"
#include "..//include//fileOperations.h"

#define BUFSIZE 200
#define CMDSIZE 30
#define PARSIZE 100

char filename_g[70];
// mount_flag = 0 => not mounted
// mount_flag = 1 => mounted
int mount_flag = 0;
int createvfs_flag = 0;

void createvfs ( char *P1, int P2 );
void mountvfs ( char *P1 );
void unmountvfs ( char *P1 );
void makedir ( char *P1, char *P2 );
void deletedir ( char *P1 );
void movedir ( char *P1, char *P2 );
void listdir ( char *P1, int P2, char *P3 );
void addfile ( char *P1, char *P2, char *P3 );
void listfile ( char *P1, char *P2 );
void updatefile ( char *P1, char *P2 );
void removefile ( char *P1 );
void movefile ( char *P1, char *P2 );
void copyfile ( char *P1, char *P2 );
void exportfile ( char *P1, char *P2 );
void searchfile ( char *P1, char *P2 );

void processcommand( char *command, char *P1, char *P2, char *P3 );

int main( int argc, char *argv[] )
{
	FILE *scriptfp;
	char linebuffer[BUFSIZE];
	char command[CMDSIZE], par1[PARSIZE], par2[PARSIZE], par3[PARSIZE];
	char *token;

	if( argc != 2 ){
		fprintf(stderr,"Usage: vfsdriver <scriptfile>\n");
		return(1);
	}

	if( (scriptfp=fopen(argv[1],"r")) == NULL ){
		fprintf(stderr,"Unable to open script file: %s\n", argv[1]);
		return(2);
	}

	while( fgets(linebuffer, sizeof(linebuffer), scriptfp) != NULL ){
		/* This output is for debugging... do not uncomment in final version */
		/*
		printf("==================================================\n");
		printf("Processing: %s", linebuffer);
		printf("==================================================\n");
		*/

		/* Remove the extra newline character in the end of line */
		linebuffer[ strlen(linebuffer)-1 ] = '\0';
		
		/* Get the command and the parameters using tokenizer */
		strcpy( command, (token = strtok(linebuffer, " ")) == NULL ? "" : token );
		
		strcpy( par1, (token = strtok(NULL, " ")) == NULL ? "" : token );
		strcpy( par2, (token = strtok(NULL, " ")) == NULL ? "" : token );
		strcpy( par3, (token = strtok(NULL, " ")) == NULL ? "" : token );
/*		printf("Command:%s:p1:%s:p2:%s:p3:%s\n",command, par1, par2, par3);*/

		processcommand( command, par1, par2, par3 );
	}
}

void processcommand( char *command, char *P1, char *P2, char *P3 )
{
	if( strcmp(command, "createvfs") == 0 ){
		int size = atoi(P2);
		createvfs (P1,size);
	}
	else if( strcmp(command, "mountvfs") == 0 )
		mountvfs (P1);
	else if( strcmp(command, "unmountvfs") == 0 )
		unmountvfs (P1);
	else if( strcmp(command, "makedir") == 0 )
		makedir (P1,P2);
	else if( strcmp(command, "deletedir") == 0 )
		deletedir (P1);
	else if( strcmp(command, "movedir") == 0 )
		movedir (P1,P2);
	else if( strcmp(command, "listdir") == 0 ){
		int flag = atoi(P2);
		listdir (P1,flag,P3);
	}
	else if( strcmp(command, "addfile") == 0 )
		addfile (P1,P2,P3);
	else if( strcmp(command, "listfile") == 0 )
		listfile (P1,P2);
	else if( strcmp(command, "updatefile") == 0 )
		updatefile (P1,P2);
	else if( strcmp(command, "removefile") == 0 )
		removefile (P1);
	else if( strcmp(command, "movefile") == 0 )
		movefile (P1,P2);
	else if( strcmp(command, "copyfile") == 0 )
		copyfile (P1,P2);
	else if( strcmp(command, "exportfile") == 0 )
		exportfile (P1,P2);
	else if( strcmp(command, "searchfile") == 0 )
		searchfile (P1,P2);
	else
		printf("Ignoring invalid command %s\n", command);
}

void createvfs ( char *P1, int P2 )
{
	strcat(filename_g,"");
	strcpy(filename_g,P1);
	strcat(filename_g,".dat");
	/*createvfs_flag = createVFS(P1, P2, filename_g);*/
	free(root);
	if(createVFS(P1, P2, filename_g))
		printf("createvfs_SUCCESS\n");
}

void mountvfs ( char *P1 )
{	
	if(mount_flag == 1 /*|| createvfs_flag == 0*/) {
		printf("mountvfs_FAILURE %s\n",ERR_VFS_MOUNT_03);
		return;
	}
	
	if(mount_nary(P1, filename_g) && mount_hash(P1,filename_g)) {
		printf("mountvfs_SUCCESS\n");
		mount_flag = 1;
	}
	
}

void unmountvfs ( char *P1 )
{
	if(mount_flag == 0) {
		printf("unmountvfs_FAILURE %s\n",ERR_VFS_UNMOUNT_03);
		return;
	}
	if(unmntvfs(P1, filename_g)) {
		printf("unmountvfs_SUCCESS\n");
		mount_flag = 0;
	}
}

void makedir ( char *P1, char *P2 )
{
	if(mount_flag == 0) {
		printf("makedir_FAILURE %s\n",ERR_VFS_MAKEDIR_05);
		return;
	}
	if(makedirectory(P1, P2, filename_g)) {
		printf("makedir_SUCCESS\n");
	}
}

void deletedir ( char *P1 )
{
	if(mount_flag == 0) {
		printf("deletedir_FAILURE %s\n",ERR_VFS_DELETEDIR_04);
		return;
	}
	if(deletedirectory(P1, filename_g)) {
		printf("deletedir_SUCCESS\n");
	}
}

void movedir ( char *P1, char *P2 )
{
	if(mount_flag == 0) {
		printf("movedir_FAILURE %s\n",ERR_VFS_MOVEDIR_08);
		return;
	}
	if(movedirectory(P1, P2, filename_g)) {
		printf("movedir_SUCCESS\n");
	}
}

void listdir ( char *P1, int P2, char *P3 )
{
	if(mount_flag == 0) {
		printf("listdir_FAILURE %s\n",ERR_VFS_LISTDIR_03);
		return;
	}
	if(listdirectory(P1, P2, P3)) {
		printf("listdir_SUCCESS\n");
	}
}

void addfile ( char *P1, char *P2, char *P3 )
{	
	if(mount_flag == 0) {
		printf("addfile_FAILURE %s\n",ERR_VFS_ADDFILE_07);
		return;
	}
	if(addfileop(P1, P2, P3, filename_g)) {
		printf("addfile_SUCCESS\n");
	}
	
}

void listfile ( char *P1, char *P2 )
{
	if(mount_flag == 0) {
		printf("listfile_FAILURE %s\n",ERR_VFS_LISTFILE_04);
		return;
	}
	if(listfileop(P1,P2,filename_g))
		printf("listfile_SUCCESS\n");
}

void updatefile ( char *P1, char *P2 )
{
	if(mount_flag == 0) {
		printf("updatefile_FAILURE %s\n",ERR_VFS_UPDATEFILE_04);
		return;
	}
	if(updatefileop(P1,P2,filename_g))
		printf("updatefile_SUCCESS\n");
}

void removefile ( char *P1 )
{	
	if(mount_flag == 0) {
		printf("removefile_FAILURE %s\n",ERR_VFS_REMOVEFILE_02);
		return;
	}
	if(removefileop(P1, filename_g)) {
		printf("removefile_SUCCESS\n");
	}
}

void movefile ( char *P1, char *P2 )
{
	if(mount_flag == 0) {
		printf("movefile_FAILURE %s\n",ERR_VFS_MOVEFILE_06);
		return;
	}
	if(movefileop(P1, P2, filename_g)) {
		printf("movefile_SUCCESS\n");
	}
}

void copyfile ( char *P1, char *P2 )
{
	if(mount_flag == 0) {
		printf("copyfile_FAILURE %s\n",ERR_VFS_COPYFILE_05);
		return;
	}
	if(copyfileop(P1, P2, filename_g)) {
		printf("copyfile_SUCCESS\n");
	}
}

void exportfile ( char *P1, char *P2 )
{
	if(mount_flag == 0) {
		printf("exportfile_FAILURE %s\n",ERR_VFS_EXPORTFILE_04);
		return;
	}
	if(exportfileop(P1,P2,filename_g))
		printf("exportfile_SUCCESS\n");
}

void searchfile ( char *P1, char *P2 )
{
	if(mount_flag == 0) {
		printf("searchfile_FAILURE %s\n",ERR_VFS_SEARCHFILE_02);
		return;
	}
	if(searchfileop1(P1,P2,filename_g))
		printf("searchfile_SUCCESS\n");
}


